package com.java78.addon.journal.directive;

import com.java78.addon.journal.model.JpressAddonJournal;
import com.java78.addon.journal.service.JpressAddonJournalService;
import com.jfinal.aop.Inject;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;

/**
 * @description: 点击点赞或者倒彩
 * @author: linsan
 * @create: 2020-03-21 12:51
 **/
@JFinalDirective("JournalClick")
public class JournalClickDirective extends JbootDirectiveBase {

	@Inject
	private JpressAddonJournalService service;

	@Override
    public void onRender(Env env, Scope scope, Writer writer) {
    	Object jid = getPara("jid", scope);//获取到id
    	Object type = getPara("type", scope);//获取点击类型
    	JpressAddonJournal entry = service.findById(jid);
    	if(type !=null && entry != null) {
    		if(type.toString().equals("catcallCount")) {
    	    	entry.setCatcallCount(entry.getCatcallCount() == null ? 1 : entry.getCatcallCount()+1);
    	    	scope.setLocal("count", entry.getCatcallCount());
    		}else {
    			entry.setThumbsupCount(entry.getThumbsupCount() == null ? 1 : entry.getThumbsupCount()+1);	
    			scope.setLocal("count", entry.getThumbsupCount());
    	    }
    		service.saveOrUpdate(entry);
    	}
        scope.setLocal("type", type.toString());
        renderBody(env, scope, writer);
    }
	
	@Override
	public boolean hasEnd() {
		return true;
	}
}
